package jNotesGUI;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;


public class NoteListener implements MouseListener, MouseMotionListener, PropertyChangeListener
{
	private JButton Note;
	private Main Main;
	
	private final int offSetX = 10;
	private final int offSetY = 50;
	
	private final int ScreenOffSetTop = 40;//40 pixels from top of window to actual writting area;
	private int ScreenOffSetRight = 0;//Not final due to posible movement of sidebar.
	
	public NoteListener(JButton note, Main main)
	{
		Note = note;
		Main = main;
	}

	public void mouseClicked(MouseEvent e) 
	{
	
	}

	public void mouseEntered(MouseEvent e) 
	{		
	}

	public void mouseExited(MouseEvent e) 
	{
	}

	public void mousePressed(MouseEvent e) 
	{
	}

	public void mouseReleased(MouseEvent e) 
	{
	}

	public void mouseDragged(MouseEvent e) 
	{
		boolean Edge;
		
		try
		{
			Edge = edgeDetect();
		}
		catch(NullPointerException a)
		{
			Edge = false;
		}
		
		if(!Edge)
		{
			//Point newLocation = new Point(Main.getMousePosition().x - offSetX, Main.getMousePosition().y - offSetY);
			try
			{
				int x = Main.getMousePosition().x;// + (Main.getMousePosition().x - Note.getLocation().x);
				int y = Main.getMousePosition().y;// + (Main.getMousePosition().y - Note.getLocation().y);
				
				Point newLocation = new Point(x - offSetX, y - offSetY);
				//Debugging
				//System.out.println("The mouse position is: " + Main.getMousePosition().x + "," + Main.getMousePosition().y);
				//System.out.println("The Item's position currently is: " + Note.getLocation().x + "," + Note.getLocation().y);
				//End Debugging
				
				Note.setLocation(newLocation);
			}
			catch(Exception E)
			{
				/*
				System.out.println("Mouse Out of Bounds. Expanding Area.");
				Main.Panel.setPreferredSize(new Dimension(Main.Panel.getSize().height, Main.Panel.getSize().width + 20));
				Main.repaint();

				int index = Main.getNoteIndex();
				Main.addNote();
				Main.removeNote(index);
				 */
				/*
				int index = Main.getNoteIndex();
				Main.addNote();
				Main.removeNote(index);
				*/

			}
			//System.out.println("The Item's new position is: " + newLocation.toString());
		}
		else
		{
			System.err.println("Out of Bounds");
			
		}
		Main.repaint();
	}

	public void mouseMoved(MouseEvent e) 
	{
	}

	public void propertyChange(PropertyChangeEvent evt) 
	{
		System.out.println("Event Occured");
	}
	
	public boolean edgeDetect()
	{
		Point Location = Main.getLocation();
		
		
		//Debug
		//System.out.println("Location: " + Location);
		//End Debug
		
		Point Mouse = Main.getMousePosition();
		
		/**
		* The top edge is determined by the position of the top right edge plus the 
		* offset with the toolbar and file menus. 
		* NOTE: This is on the Y Axis only.
		*/
		int TopEdge = (Location.y+ScreenOffSetTop);
		
		/**
		 * The Bottom Edge is determined by the location of the top right edge plus the height
		 * of the container. This is ignoring the window border to allow moving windows to the
		 * bottom.
		 * NOTE: This is on the Y Axis only.
		 */
		int BottomEdge = (Location.y+Main.getHeight());
		
		/**
		 * The Right Edge is determined by the top right point and using its position. This is
		 * ignoring the screen border to allow expanding to the right side.
		 * NOTE: This is on the X Axis only.
		 */
		int RightEdge = (Location.x);
		
		/**
		 * The Left Edge is determined by the top right corner's X position and adding it the 
		 * windows width. This is ignoring the borders to allow expanding the container to the
		 * left side.
		 * NOTE: This is on the X Axis only.
		 */
		int LeftEdge = (Location.x+Main.getWidth());
		
		//Debugging
		//System.out.println("Results: ");
		//System.out.println("Mouse Position: " + Mouse.x + "," + Mouse.y);
		//System.out.println("Window Position and Borders: " + "Top " + TopEdge + " Bottom " + BottomEdge + " Right " + RightEdge + " Left " + LeftEdge);
		//End Debugging
		
		if(Mouse.y < TopEdge)
			return true;
		else if(Mouse.y > BottomEdge)
			return true;
		/*
		else if(Mouse.x < RightEdge)
			return true;
		else if(Mouse.x > LeftEdge)
			return true;
		*/
		else	
			return false;
	}
	
	public void setRightOffset(int offset)
	{
		if(Main.isAccesing())
			ScreenOffSetRight = offset;
	}

}
